package com.pr.memcache;

import net.spy.memcached.MemcachedClient;
import java.net.InetSocketAddress;
import java.util.concurrent.Future;

/**
 * @author pr
 */
public class MemcacheExample {
    public static void main(String[] args) {
        try {
            // 创建 Memcached 客户端并连接到服务器
            MemcachedClient memcachedClient = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));

            // 存储数据
            Future<Boolean> future = memcachedClient.set("key_name", 60, "some_value");

            // 检查存储是否成功
            if (future.get()) {
                System.out.println("数据已缓存成功");
            }

            // 读取数据
            Object value = memcachedClient.get("key_name");
            if (value != null) {
                System.out.println("从缓存获取的数据: " + value);
            } else {
                System.out.println("缓存未命中");
            }

            // 删除缓存
            memcachedClient.delete("key_name");

            // 关闭连接
            memcachedClient.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}